package logic

import (
	"context"
	"errors"
	"gitignore/models/mysql"

	"gitignore/gitignorerpc/gitignorerpc"
	"gitignore/gitignorerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *gitignorerpc.UserUpdateRequest) (*gitignorerpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	err := user.FindUserByUpdate(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("重置密码失败")
	}

	if in.Password != in.NewPassword {
		return nil, errors.New("密码不能一直，请重新更改")

	}
	user.Password = in.Password
	return &gitignorerpc.UserUpdateResponse{
		UserId: int64(user.ID),
	}, nil
}
